package v3.object.space;

import galaxy.obj.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Sector
{
	Point coordinate;
	iSpaceObj[][] sector;
	ArrayList<iSpaceObj> list = new ArrayList<iSpaceObj>();
	HashMap<SpaceObjType, ArrayList<iSpaceObj>> map = new HashMap<SpaceObjType, ArrayList<iSpaceObj>>();

	public Sector(Point coordinate)
	{
		this.coordinate = coordinate;
		sector = new iSpaceObj[10][10];
		list = new ArrayList<iSpaceObj>();
		inzializeMap();
	}

	private void inzializeMap()
	{

		map = new HashMap<SpaceObjType, ArrayList<iSpaceObj>>();

		for (SpaceObjType o : SpaceObjType.values())
		{
			ArrayList<iSpaceObj> l = new ArrayList<iSpaceObj>();
			map.put(o, l);
		}

	}

	public void setISpaceObj(Point sectorPoint, iSpaceObj o) throws Exception
	{
		int x = sectorPoint.getiX();
		int y = sectorPoint.getiY();
		if (sector[x][y] != null)
			throw new Exception("Sector Point already occupy - point:" + sectorPoint + " contain " + o);

		sector[x][y] = o;

		list.add(o);

		if (map.get(o.getType()) == null)
		{
			ArrayList<iSpaceObj> l = new ArrayList<iSpaceObj>();
			map.put(o.getType(), l);
		}

		map.get(o.getType()).add(o);
	}

	public Point getFreeSectorPoint()
	{
		Point res = null;
		int tempt = 0;
		do
		{
			tempt++;
			res = new Point(10, true);
			if (tempt > 1000)
				return null;
		}
		while (getISpaceObject(res) != null);

		return res;
	}

	public iSpaceObj getISpaceObject(Point res)
	{
		return sector[res.getiX()][res.getiY()];
	}

	public iSpaceObj[][] getSector()
	{
		return sector;
	}

	public void setSector(iSpaceObj[][] sector)
	{
		this.sector = sector;
	}

	public ArrayList<iSpaceObj> getList()
	{
		return list;
	}

	public void setList(ArrayList<iSpaceObj> list)
	{
		this.list = list;
	}

	public HashMap<SpaceObjType, ArrayList<iSpaceObj>> getMap()
	{
		return map;
	}

	public void setMap(HashMap<SpaceObjType, ArrayList<iSpaceObj>> map)
	{
		this.map = map;
	}

	public ArrayList<iSpaceObj> getISpaceObjList(SpaceObjType t)
	{
		return map.get(t);
	}

	public void updateSector(ArrayList<iSpaceObj> list) throws Exception
	{
		sector = new iSpaceObj[10][10];
		list = new ArrayList<iSpaceObj>();
		map = new HashMap<SpaceObjType, ArrayList<iSpaceObj>>();
		for (iSpaceObj o : list)
			setISpaceObj(o.getSectorPosition(), o);
	}

	public int[] getInfo(SpaceObjType... objTypes)
	{
		int[] res = new int[objTypes.length];

		for (int i = 0; i < objTypes.length; i++)
		{
			SpaceObjType t = objTypes[i];
			List<iSpaceObj> l = map.get(t);
			int k = 0;
			if (l != null)
				k = l.size();
			res[i] = k;
		}

		return res;
	}

	public String getInfoString(SpaceObjType... objTypes)
	{
		String info = "";
		int[] infoInt = getInfo(objTypes);
		for (int n : infoInt)
			info = info + n;

		return info;
	}

	public String[][] getSectorString()
	{
		String[][] res = new String[10][10];
		for (int x = 0; x < 10; x++)
		{
			for (int y = 0; y < 10; y++)
			{
				iSpaceObj o = sector[x][y];
				if (o != null)
					res[x][y] = SpaceObjType.getLetter(o.getType());
			}
		}
		return res;
	}

	public int getEnemyAmount()
	{
		return map.get(SpaceObjType.Enemy).size();
	}

	public boolean isEmpty()
	{
		return list.size()==0;
	}
}
